package com.mycompany.restcliclient;

import com.martiansoftware.jsap.JSAPException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.UnknownHostException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * author Ondrej Sotolar
 *
 */
public class App {

    private static int doPost(URL url, Map<String, String> params) throws UnsupportedEncodingException, IOException {

        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url.toString());

        if (!params.isEmpty()) {
            for (Entry<String, String> entry : params.entrySet()) {
                method.addParameter(entry.getKey(), entry.getValue());
            }
        }

        int statusCode = client.executeMethod(method);
        InputStream rstream = null;
        rstream = method.getResponseBodyAsStream();

        return statusCode;
    }

    private static int doDelete(URL url) throws UnsupportedEncodingException, IOException {
        HttpClient client = new HttpClient();

        DeleteMethod method = new DeleteMethod(url.toString());
        int statusCode = client.executeMethod(method);

        InputStream rstream = null;
        rstream = method.getResponseBodyAsStream();

        return statusCode;
    }

    private static int doGet(URL url) throws UnsupportedEncodingException, IOException {
        HttpClient client = new HttpClient();

        GetMethod method = new GetMethod(url.toString());
        int statusCode = client.executeMethod(method);

        InputStream rstream = null;
        rstream = method.getResponseBodyAsStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
        return statusCode;
    }

    public static void main(String[] args) throws JSAPException, UnknownHostException, IOException {

        // Server test
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:4080/pa165/index.jsp/").openConnection();
        con.setRequestMethod("HEAD");
        try {
            String result = String.valueOf(con.getResponseCode() == HttpURLConnection.HTTP_OK);
            if (result.contains("false")) {
                System.out.println("Server is down :(");
                return;
            } else {
                System.out.println("Server is running");
                
                // REST TestApp start
                CLIAppStart();
            }
        } catch (IOException e) {
            System.out.println("Server is down :(");
        }
    }

    private static void CLIAppStart() throws UnsupportedEncodingException, IOException {
        boolean quit = false;
        String restUrl = "http://localhost:4080/pa165/index.jsp/Rest/";
        while (!quit) {
            System.out.println("\n\n\n");
            System.out.println("Choose Action:");
            System.out.println("1: Courier Managment");
            System.out.println("2: Customer Managment");
            System.out.println("q: Quit");

            String main = (new Scanner(System.in)).next();

            switch (main) {
                case "1": {
                    System.out.println("\n");
                    System.out.println("Courier: Choose Action:");
                    System.out.println("1: Find all");
                    System.out.println("2: Create");
                    System.out.println("3: Find by Id");
                    System.out.println("4: Delete");
                    System.out.println("5: Update");
                    System.out.println("q: Back");

                    String sub1 = (new Scanner(System.in)).next();

                    switch (sub1) {
                        case "1": {
                            doGet(new URL(restUrl + "Courier/?findall"));
                            break;
                        }
                        case "2": {
                            HashMap params = new HashMap();

                            System.out.println("Enter first name:");
                            String fn = (new Scanner(System.in)).next();
                            params.put("firstName", fn);
                            System.out.println("Enter last name:");
                            String ln = (new Scanner(System.in)).next();
                            params.put("lastName", ln);
                            System.out.println("Enter username:");
                            String un = (new Scanner(System.in)).next();
                            params.put("username", un);

                            doPost(new URL(restUrl + "Courier/?create"), params);
                            break;
                        }
                        case "3": {
                            System.out.println("Enter id:");
                            String id = (new Scanner(System.in)).next();

                            doGet(new URL(restUrl + "Courier/?findbyid=" + id));
                            break;
                        }
                        case "4": {
                            System.out.println("Enter id:");
                            String id = (new Scanner(System.in)).next();

                            doDelete(new URL(restUrl + "Courier/?delete=" + id));
                            break;
                        }
                        case "5": {
                            HashMap params = new HashMap();

                            System.out.println("Enter id:");
                            String id = (new Scanner(System.in)).next();
                            params.put("id", id);
                            System.out.println("Enter first name:");
                            String fn = (new Scanner(System.in)).next();
                            params.put("firstName", fn);
                            System.out.println("Enter last name:");
                            String ln = (new Scanner(System.in)).next();
                            params.put("lastName", ln);
                            System.out.println("Enter username:");
                            String un = (new Scanner(System.in)).next();
                            params.put("username", un);

                            doPost(new URL(restUrl + "Courier/?update"), params);
                            break;
                        }
                        case "q": {
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                    break;
                }
                case "2": {
                    System.out.println("\n");
                    System.out.println("Customer: Choose Action:");
                    System.out.println("1: Find all");
                    System.out.println("2: Create");
                    System.out.println("3: Find by Id");
                    System.out.println("4: Find by first name");
                    System.out.println("5: Find by last name");
                    System.out.println("6: Find by phone number");
                    System.out.println("7: Find by email");
                    System.out.println("8: Delete");
                    System.out.println("9: Update");
                    System.out.println("q: Back");

                    String sub1 = (new Scanner(System.in)).next();

                    switch (sub1) {
                        case "1": {
                            doGet(new URL(restUrl + "Customer/?findall"));
                            break;
                        }
                        case "2": {
                            HashMap params = new HashMap();

                            System.out.println("Enter first name:");
                            String fn = (new Scanner(System.in)).next();
                            params.put("firstName", fn);
                            System.out.println("Enter last name:");
                            String ln = (new Scanner(System.in)).next();
                            params.put("lastName", ln);
                            System.out.println("Enter street:");
                            String st = (new Scanner(System.in)).next();
                            params.put("street", st);
                            System.out.println("Enter street number:");
                            String sn = (new Scanner(System.in)).next();
                            params.put("streetNumber", sn);
                            System.out.println("Enter city:");
                            String ct = (new Scanner(System.in)).next();
                            params.put("city", ct);
                            System.out.println("Enter postal code:");
                            String pc = (new Scanner(System.in)).next();
                            params.put("postalCode", pc);
                            System.out.println("Enter phone number:");
                            String pn = (new Scanner(System.in)).next();
                            params.put("phoneNumber", pn);
                            System.out.println("Enter email:");
                            String em = (new Scanner(System.in)).next();
                            params.put("email", em);

                            doPost(new URL(restUrl + "Customer/?create"), params);
                            break;
                        }
                        case "3": {
                            System.out.println("Enter id:");
                            String id = (new Scanner(System.in)).next();
                            doGet(new URL(restUrl + "Customer/?findbyid=" + id));
                            break;
                        }
                        case "4": {
                            System.out.println("Enter first name:");
                            String id = (new Scanner(System.in)).next();
                            doGet(new URL(restUrl + "Customer/?findByFirstName=" + id));
                            break;
                        }
                        case "5": {
                            System.out.println("Enter last name:");
                            String id = (new Scanner(System.in)).next();
                            doGet(new URL(restUrl + "Customer/?findByLastName=" + id));
                            break;
                        }
                        case "6": {
                            System.out.println("Enter phone number:");
                            String id = (new Scanner(System.in)).next();
                            doGet(new URL(restUrl + "Customer/?findByPhoneNumber=" + id));
                            break;
                        }
                        case "7": {
                            System.out.println("Enter email:");
                            String id = (new Scanner(System.in)).next();
                            doGet(new URL(restUrl + "Customer/?findByEmail=" + id));
                            break;
                        }
                        case "8": {
                            System.out.println("Enter id:");
                            String id = (new Scanner(System.in)).next();
                            doDelete(new URL(restUrl + "Customer/?delete=" + id));
                            break;
                        }
                        case "9": {
                            HashMap params = new HashMap();

                            System.out.println("Enter id:");
                            String id = (new Scanner(System.in)).next();
                            params.put("id", id);
                            System.out.println("Enter first name:");
                            String fn = (new Scanner(System.in)).next();
                            params.put("firstName", fn);
                            System.out.println("Enter last name:");
                            String ln = (new Scanner(System.in)).next();
                            params.put("lastName", ln);
                            System.out.println("Enter street:");
                            String st = (new Scanner(System.in)).next();
                            params.put("street", st);
                            System.out.println("Enter street number:");
                            String sn = (new Scanner(System.in)).next();
                            params.put("streetNumber", sn);
                            System.out.println("Enter city:");
                            String ct = (new Scanner(System.in)).next();
                            params.put("city", ct);
                            System.out.println("Enter postal code:");
                            String pc = (new Scanner(System.in)).next();
                            params.put("postalCode", pc);
                            System.out.println("Enter phone number:");
                            String pn = (new Scanner(System.in)).next();
                            params.put("phoneNumber", pn);
                            System.out.println("Enter email:");
                            String em = (new Scanner(System.in)).next();
                            params.put("email", em);

                            doPost(new URL(restUrl + "Customer/?update"), params);
                            break;
                        }
                        case "q": {
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                    break;
                }
                case "q": {
                    quit = true;
                    break;
                }
                default: {
                    break;
                }
            }
        }
    }
}
